<script setup>
//导入模型解析构造函数
import { Application } from '@splinetool/runtime';

import { onMounted, ref, } from 'vue'
import { useBigScreenMember, useBarChart, usePieChart } from '@/buz/bigScreen';

import LoadingComponent from '@/components/LoadingComponent.vue';
import VScaleScreen from 'v-scale-screen'

const { parkInfo, getParkInfo } = useBigScreenMember()
const { barChart, initBarChart } = useBarChart()
const { pieChart, initPieChart } = usePieChart()

const isloading = ref(false)

//这里使用npm而不是cdn的方式安装echarts.具体可以看官网https://echarts.apache.org/handbook/zh/basics/import/

//初始化3d模型
const ref3d = ref(null)
const init3dModel = () => {
  isloading.value = true
  //实例化解析器实例
  const spline = new Application(ref3d.value)
  //拉取模型--这个是后端提供的
  spline.load('https://fe-hmzs.itheima.net/scene.splinecode').then(() => {
    console.log('3d模型加载并渲染完毕');

  }).finally(()=>{
    //promise的finally方法,不管成功还是失败,都会执行
isloading.value = false
  })
}

onMounted(async () => {
  //在这里进行echarts的配置.因为echarts需要操作DOM
  //对前面发请求的优化--补充,可以将请求放在onMounted里面,这样就可以保证DOM已经渲染完毕.
  //⁉️ 这样等到有结果之后再渲染图表.,这样不会报错,但是图也渲染不出来.因为onMounted只会执行一次
  //  parkInfo.value.parkIncome&& initBarChart()

  //优化,将请求也放到onMountd
  //调用接口,会发现没有token,所以相应401.这个需要登陆后端的
  await getParkInfo()
  //💕 重点关注echarts函数执行的时机,在DOM渲染好,数据获取后
  initBarChart()
  initPieChart()
  init3dModel()

})





</script>


<template>

<!-- //使用大屏可视化的匹配插件,对于不同的设备有一个最佳匹配尺寸,需要自己调整 -->
<v-scale-screen width="1400" height="800">
  <div class="all-charts">
    <!-- 园区概况 -->
    <!--💕  版本3,0判断是空对象,那么就不渲染这个section -->
    <div class="section-one" v-if="Object.keys(parkInfo).length > 0">
      <img class="img-header"
        src="https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/smartPark/%E5%A4%A7%E5%B1%8F%E5%88%87%E5%9B%BE/%E5%9B%AD%E5%8C%BA%E6%A6%82%E5%86%B5%402x.png"
        alt="" />
      <div class="icons-container">
        <div class="item">
          <div class="icons-item building-icon">
            <span class="number">
              <!--版本1.0 直接按照下面的写法,模版 渲染的时候,parkInfo是空对象-->
              <!-- {{ parkInfo.base.buildingTotal}} -->

              <!-- 版本2.0 判断对象是否有值 -->
              <!-- {{parkInfo.base && parkInfo.base.buildingTotal }} -->

              {{ parkInfo.base.buildingTotal }}

              <!-- 版本4 可选链,有值才往后取值.并没有减少渲染次数,当请求拿到值之后,数据更新,需要再次渲染 -->
              <!-- {{ parkInfo.base?.buildingTotal }} -->

            </span>

          </div>

          <span class="title">楼宇总数</span>

          <span class="unity">（栋）</span>

        </div>

        <div class="item">
          <div class="icons-item enterprise-icon">
            <span class="number">
              {{ parkInfo.base.enterpriseTotal }}
            </span>

          </div>

          <span class="title">入驻企业总数</span>

          <span class="unity">（家）</span>

        </div>

        <div class="item">
          <div class="icons-item car-icon">
            <span class="number">
              {{ parkInfo.base.parkingTotal }}
            </span>

          </div>

          <span class="title">车位总数</span>

          <span class="unity">（个）</span>

        </div>

        <div class="item">
          <div class="icons-item rod-icon">
            <span class="number">
              {{ parkInfo.base.chargePoleTotal }}
            </span>

          </div>

          <span class="title">一体杆总数</span>

          <span class="unity">（个）</span>

        </div>

      </div>

    </div>

    <!-- 园区年度收入分析 -->
    <div class="section-two">
      <img class="img-header"
        src="https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/smartPark/%E5%A4%A7%E5%B1%8F%E5%88%87%E5%9B%BE/%E5%9B%AD%E5%8C%BA%E5%B9%B4%E5%BA%A6%E6%94%B6%E5%85%A5%E5%88%86%E6%9E%90%402x.png"
        alt="" />
      <div class="bar-chart-titile">
        <span>单位：元</span>

        <div>
          <span class="bar-icon blue-bar-icon"></span>

          <span class="bar-icon red-bar-icon"></span>

          收入情况
        </div>

      </div>

      <div class="bar-chart" ref="barChart"></div>

    </div>

    <!-- 园区产业分布 -->
    <div class="section-three">
      <img class="img-header"
        src="https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/smartPark/%E5%A4%A7%E5%B1%8F%E5%88%87%E5%9B%BE/%E5%9B%AD%E5%8C%BA%E4%BA%A7%E4%B8%9A%E5%88%86%E5%B8%83%402x.png"
        alt="" />
      <div class="pie-chart" ref="pieChart"></div>

    </div>


  </div>

  <div class="model-container">
    <LoadingComponent :loading="isloading"></LoadingComponent>
      <!-- 准备3D渲染节点 -->
      <canvas class="canvas-3d" ref="ref3d" />

    </div>
</v-scale-screen>



</template>

<style lang="scss">
.all-charts {
  position: absolute;
  top: 0;
  left: 0;
  width: 480px;
  height: 100vh;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: linear-gradient(to left, rgba(0, 6, 15, 0.00) 0%, rgba(0, 6, 15, 0.00) 20%, rgba(0, 0, 0, 0.40) 30%, rgba(0, 0, 0, 0.60) 40%, rgba(1, 4, 11, 1) 70%, #04070d 100%);

  .img-header {
    height: 30px;
  }
}

.section-one {
  flex-basis: 25%;

  .icons-container {
    display: flex;
    justify-content: space-between;
    padding: 20px 0;

    .item {
      text-align: center;
      display: flex;
      flex-direction: column;
      flex: 1;

      .icons-item {
        height: 80px;
        position: relative;

        .number {
          position: absolute;
          left: 50%;
          transform: translateX(-50%);
          font-size: 18px;
          font-family: FontquanXinYiGuanHeiTi, FontquanXinYiGuanHeiTi-Regular;
          color: #ffffff;
        }
      }

      .building-icon {
        background: url('@/assets/building-icon.png') no-repeat 50% 0 / contain;
      }

      .enterprise-icon {
        background: url('@/assets/enterprise-icon.png') no-repeat 50% 0 / contain;
      }

      .rod-icon {
        background: url('@/assets/rod-icon.png') no-repeat 50% 0 / contain;
      }

      .car-icon {
        background: url('@/assets/car-icon.png') no-repeat 50% 0 / contain;
      }

      .title,
      .unity {
        font-size: 14px;
        color: #cdd7e1;
      }

      .title {
        margin-top: 8px;
      }
    }
  }
}

.section-two {
  flex-basis: 35%;
  margin-top: 50px;

  .bar-chart {
    width: 100%;
    height: calc(100% - 90px);
  }
}

.section-three {
  flex-basis: 40%;

  .pie-chart {
    position: relative;
    margin: 0 auto;
    padding-bottom: 20px;
    width: 80%;
    height: calc(100% - 40px);
  }
}

.model-container {
  height: 100%;
  background-color: black;
  width: 100%;
  flex-shrink: 0;
}
</style>
